var valueInput = GetQueryString("valueInput"),nameInput = GetQueryString("nameInput");
var layerIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引
$(document).ready(function(){

	
	$(parent.window).resize(function(){
		setTimeout(function(){
			parent.$('.layui-layer').css('top','0px')
		}, 100);
	})
	setxmSelectTree();
	$('#backButton,#commitButton').on('click',function(){
//		alert(parent.$(".layui-layer").scrollTop()+" px");
//		alert(dd);
//		dd.ready(function(){
//			dd.biz.navigation.goBack({
//			    onSuccess : function(result) {
//			        /*result结构
//			        {}
//			        */
//			    },
//			    onFail : function(err) {}
//			})
//		});
		parent.layer.close(layerIndex);
//		history.back(-1);
//		history.go(-1)
//		 window.close();
	});
	
});
var dingdingUserData = [];
function setxmSelectTree(){
	$.ajax({
		url: "../../adminmanager/user/getUserTreeSelect"
		,type:'post'
		,async: false
		,contentType: "application/json; charset=utf-8"
//		,data:JSON.stringify({})
		,dataType:'json'
		,success: function(res){
			dingdingUserData = res.data;
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
		}
		
	});
	var userTree = xmSelect.render({
		el: '#demo1',
		autoRow: true,
		filterable: true,
		theme: {
			color: '#669fc7',
		},
		style: {
			marginTop: '2px',
			borderRadius: '6px',
		},
		tree: {
			show: true,
			simple: false,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -3 ],
		},
		model: {
			label: {
				type: 'block',
				block: {
					//最大显示数量, 0:不限制
					showCount: 1,
					//是否显示删除图标
					showIcon: true,
				}
			}
		},
		on: function(data){
			//arr:  当前多选已选中的数据
			setxmSelectValueToInput(data.arr)
		},
		toolbar: {
			show: true,
			list: ['ALL', 'REVERSE', 'CLEAR']
		},
		filterable: true,
		direction: 'down',
		height: 300,
		data:dingdingUserData
	});
	setTimeout(function(){
		userTree.opened();
		$('.xm-search-input').on('focus',function(){
			setTimeout(function(){
				parent.$('.layui-layer').css('top','0px')
			}, 100);
		});
	}, 800);
	//有数据
	var checkedUser = sessionStorage.getItem("checkedUser")
	if(checkedUser != undefined && checkedUser.length > 0 && checkedUser != 'null'){
		userTree.setValue(checkedUser.split(','));	
	}
}

//将xmselect值放到input中
//function setxmSelectValueToInput(dataArray){
//	var valueArray = [];
//	var nameArray = [];
//	for (var i = 0; i < dataArray.length; i++) {
//		valueArray.push(dataArray[i].value);
//		nameArray.push(dataArray[i].name);
//	}
//	$("#"+valueInput,window.opener.document).val(valueArray.join(','));
//	$("#"+nameInput,window.opener.document).val(nameArray.join(','));
//}
//将xmselect值放到input中
function setxmSelectValueToInput(dataArray){
	var valueArray = [];
	var nameArray = [];
	for (var i = 0; i < dataArray.length; i++) {
		valueArray.push(dataArray[i].value);
		nameArray.push(dataArray[i].name);
	}
	parent.$("#"+valueInput).val(valueArray.join(','));
	parent.$("#"+nameInput).val(nameArray.join(','));
//	console.log(valueInput)
//	console.log(valueArray.join(','))
//	console.log(parent.$("#"+valueInput).val());
//	console.log(nameInput)
//	console.log(nameArray.join(','))
//	console.log(parent.$("#"+nameInput).val());
}